package ru.vsu.amm.fuzzy.problems.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle.ComponentPlacement;

/**
 * 
 * @author Иванов Илья
 * @since 22 января 2013
 *
 */
public class HelpViewer extends JDialog {

	private static final long serialVersionUID = -7759931266976645034L;

	/**
	 * Create the dialog.
	 */
	public HelpViewer(FuzzyFrame owner, String helpHtml) {
		super(owner, false);
		
		setResizable(false);
		setTitle("Справка");
		setBounds(100, 100, 600, 419);
		JPanel htmlPanel = new JPanel();
		JPanel buttonPanel = new JPanel();
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(buttonPanel, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 560, Short.MAX_VALUE)
						.addComponent(htmlPanel, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 560, Short.MAX_VALUE))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(htmlPanel, GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(buttonPanel, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
					.addContainerGap())
		);
		htmlPanel.setLayout(null);
		
		JScrollPane scroller = new JScrollPane();
		scroller.setBounds(0, 0, 570, 320);
		htmlPanel.add(scroller);
		
		JEditorPane editor = new JEditorPane("text/html", helpHtml);
		editor.setEditable(false);
		editor.setCaretPosition(0);
		scroller.setViewportView(editor);
		JButton close = new JButton("Закрыть");
		close.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				HelpViewer.this.setVisible(false);
				HelpViewer.this.dispose();
			}
		});
		GroupLayout gl_buttonPanel = new GroupLayout(buttonPanel);
		gl_buttonPanel.setHorizontalGroup(
			gl_buttonPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_buttonPanel.createSequentialGroup()
					.addGap(238)
					.addComponent(close, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addGap(238))
		);
		gl_buttonPanel.setVerticalGroup(
			gl_buttonPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_buttonPanel.createSequentialGroup()
					.addGap(0, 0, Short.MAX_VALUE)
					.addComponent(close)
					.addContainerGap())
		);
		buttonPanel.setLayout(gl_buttonPanel);
		getContentPane().setLayout(groupLayout);
		
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setLocationRelativeTo(getOwner());
		setVisible(true);
	}
}